package com.example.demo.controller;


import com.example.demo.base.*;
import com.example.demo.entity.Complain;
import com.example.demo.entity.Cost;
import com.example.demo.entity.House;
import com.example.demo.service.CostService;
import com.example.demo.service.HouseService;
import com.example.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage"})
public class CostController {

    @Autowired
    private CostService costService;

    @Autowired
    private HouseService houseService;

    @RequestMapping("/cost/list")
    public BaseResponse list(
            @RequestParam(required = false, value = "userId") String userId,
            @RequestParam(required = false, value = "floorId") Integer floorId,
            @RequestParam(required = false, value = "unitId") Integer unitId,
            @RequestParam(required = false, value = "roomId") String roomId,
            @RequestParam(required = false, value = "chargeType") String chargeType,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request){
        QueryBuilder queryBuilder = new QueryBuilder();
        if(StringUtils.isNotBlank(userId)){
            queryBuilder.addFilter(Filter.eq("user_id",userId));

            House house = houseService.selectEntity(queryBuilder);
            if(house!=null){
                queryBuilder.clear();
                queryBuilder.addFilter(Filter.eq("floor_id",house.getFloorId()))
                        .addFilter(Filter.eq("unit_id",house.getUnitId()))
                        .addFilter(Filter.eq("room_id",house.getRoomId()));
            }else{
                return BaseResponse.success();
            }
        }
        if(floorId!=null){
            queryBuilder.addFilter(Filter.eq("floor_id",floorId));
        }
        if(unitId!=null){
            queryBuilder.addFilter(Filter.eq("unit_id",unitId));
        }
        if(StringUtils.isNotBlank(roomId)){
            queryBuilder.addFilter(Filter.eq("room_id",roomId));
        }
        if(StringUtils.isNotBlank(chargeType)){
            queryBuilder.addFilter(Filter.eq("charge_type",chargeType));
        }
        queryBuilder.addOrder(Order.desc("id"));
        Pageable<Cost> pageable = costService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);
        BaseResponse response = new BaseResponse();
        response.setRows(pageable.getRecords());
        response.setTotal(new Long(pageable.getTotal()).intValue());

        return response;
    }

    @RequestMapping("/cost/add")
    public BaseResponse add(
            @RequestParam(required = false, value = "floorId") Integer floorId,
            @RequestParam(required = false, value = "unitId") Integer unitId,
            @RequestParam(required = false, value = "roomId") String roomId,
            @RequestParam(required = false, value = "chargeType") String chargeType,
            @RequestParam(required = false, value = "money") Double money,
            HttpServletRequest request){

        Cost cost = new Cost();
        cost.setFloorId(floorId);
        cost.setUnitId(unitId);
        cost.setRoomId(roomId);
        cost.setChargeType(chargeType);
        cost.setMoney(money);
        costService.insertEntity0(cost);
        return BaseResponse.success();
    }

}
